var baseCompanyInfo = angular.module('baseCompanyInfo', [ 'paginationService', 'anajaxService', 'agularValidate', 'innerPaginationService' ]);

baseCompanyInfo.controller('BaseCompanyInfoController', function($scope, $http, pagination, anajax, innerPagination) {

	$scope.queryForm = {};
	$scope.companyInfoForm = {};
	$scope.companyModuleForm = {};
	$scope.resultList = {};
	$scope.company_module_dull_box = null;
	$scope.resourceTitle = "";
	$scope.companyName = "";

	/**
		 * 查询
		 */
	var query = function(params) {
		var url = commonutil.actionPath + '/base/company/companylist';
		anajax.doajax(url, params, function(data) {
			$scope.resultList = data.resultList;
			pagination.setData(data.pagination);
			$scope.pagination = data.pagination;
		});
	};

	query($scope.queryForm);

	$scope.doquery = function() {
		query($scope.queryForm);
	}


	/**初始化新增*/
	$scope.goAdd = function(index) {
		var iscontinue = false;
		$("#companyCode").removeAttr("readonly");
		$scope.companyInfoForm = {
		};
		$("#addDiv").show();
		$scope.companyInfoForm.isEnable = 1;
		$scope.stations = [];
		$scope.rightPart = 'add';
		
		removeTips($('#edit-data-warp'));
		pageutil.showRightBarAn('新增企业信息');
	};

	/**初始化编辑*/
	$scope.goModify = function(index) {
		$scope.rightPart = 'edit';
		var assets = $scope.resultList[index];
		angular.copy(assets, $scope.companyInfoForm);
		$("#addDiv").hide();
		$scope.companyInfoForm.username = 0;
		$scope.companyInfoForm.password = 0;
		$scope.companyInfoForm.phone = 0;
		$scope.companyInfoForm.email = 0;
		$("#companyCode").attr("readonly", "readonly");
		$scope.companyInfoForm.createTime = null;
		$scope.companyInfoForm.lastUpdateTime = null;
		removeTips($('#edit-data-warp'));
		pageutil.showRightBarAn('编辑企业信息');
	};

	$scope.doCancel = function() {
		pageutil.hideRightBar();
	}
	
	
	
	

	/**
	 * 执行添加
	 */
	$scope.doAdd = function() {
		var res = Validator.validate($("#edit-data-warp"));
		if (!res) {
			return;
		}
		if ($scope.rightPart == 'add') {
			$scope.add($scope.companyInfoForm);
		} else {
			$scope.modify($scope.companyInfoForm);
		}
	};

	/**
	 * 企业模块初始化
	 */
	$scope.assignModule = function(index) {
		$scope.resourceTitle = $scope.resultList[index].companyNameCn;
		if ($scope.company_module_dull_box == null) {

			$scope.company_module_dull_box = $("#moduleInfoIds").bootstrapDualListbox({
				nonSelectedListLabel : '未拥有模块',
				selectedListLabel : '已拥有模块',
				preserveSelectionOnMove : 'moved',
				moveOnSelect : false,
				infoText : "",
				showFilterInputs : false
			})
		}
		$scope.companyModuleForm.companyCode = $scope.resultList[index].companyCode;

		var url = commonutil.actionPath + '/module/company/form';
		$($scope.company_module_dull_box).html("");
		
		anajax.doajax(url, {
			companyCode : $scope.resultList[index].companyCode
		}, function(data) {
			//初始化企业模块双向列表
			var companyHasModules = data.companyHasModules;
			$(companyHasModules).each(function(i, item) {
				$scope.company_module_dull_box.append("<option value='" + item.moduleInfoId + "' selected>" + item.moduleName + "</option>");
			});

			var companyNotHasModules = data.companyNotHasModules;
			$(companyNotHasModules).each(function(i, item) {
				$scope.company_module_dull_box.append("<option value='" + item.moduleInfoId + "'>" + item.moduleName + "</option>");

			});
			$scope.company_module_dull_box.bootstrapDualListbox('refresh');

		});

		$("#confirm_assign_module_div").modal('show');

	}


	//保存企业模块
	$scope.saveCompanyModules = function() {
		var mids = $("#moduleInfoIds").val();
		var url = commonutil.actionPath + '/module/company/add?modules=' + mids;

		anajax.doajax(url, {
			companyCode : $scope.companyModuleForm.companyCode
		}, function(data) {});
	}



	/**
	 * 执行添加
	 */
	$scope.add = function(postData) {
		var url = commonutil.actionPath + '/base/company/add';
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue) {
				pageutil.showTip('添加成功');
				pageutil.hideRightBar();
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};

	
	/**
	 * 执行修改
	 */
	$scope.modify = function(postData) {

		var url = commonutil.actionPath + '/base/company/update';
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue) {
				pageutil.showTip('修改成功');
				pageutil.hideRightBar();
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};




	/**
	 * 弹出确认删除窗口
	 */
	$scope.showDelWin = function($index) {
		var assetBean = $scope.resultList[$index];
		$scope.delBean = assetBean;
		//$scope.delBean.useTime=null;
		$("#confirm_del_asset_div").modal('show');
	};

	$scope.clearBean = {};

	/**
	 * 弹出确认删除窗口
	 */
	$scope.showClearRedis = function($index) {
		var assetBean = $scope.resultList[$index];
		$scope.companyName = assetBean.companyName;
		$scope.clearBean = assetBean;
		//$scope.delBean.useTime=null;
		$("#confirm_Clear_Redis_div").modal('show');
	};

	$scope.clearRedis = function() {
		var url = commonutil.actionPath + '/base/company/clearRedis';
		anajax.doajax(url, {
			companyCode : $scope.clearBean.companyCode
		}, function(data) {
			if (data.resultValue) {
				pageutil.showTip('清除成功');
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};


	$scope.doDelAsset = function() {
		console.log($scope.delBean);
		var url = commonutil.actionPath + '/base/company/del';
		anajax.doajax(url, {
			id : $scope.delBean.baseCompanyInfoId
		}, function(data) {
			if (data.resultValue) {
				pageutil.showTip('删除成功');
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	$scope.delBean = {};
	
	/**
	 * 跳转到指定页
	 */
	$scope.goPage = function(selNum) {
		
		pagination.goPage(selNum);
		query($scope.pagination);
	};
	
	/**
	 * 上一页
	 */
	$scope.lastPage = function() {
		pagination.lastPage();
		query($scope.pagination);
	};
	
	/**
	 * 下一页
	 */
	$scope.nextPage = function() {
		pagination.nextPage();
		query($scope.pagination);
	};
});